﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace YArchitech.HVAC
{
	public class BuildingSystem
	{
		public BuildingSystem(int type = 0)
		{
			this.connType = type;
			this.risePipes = new List<RisePipeSystem>();
			this.supplyPipes = new PipeSet();
			this.backPipes = new PipeSet();
		}

		public string Name
		{
			get
			{
				return "室内系统";
			}
		}

		public int ConnectType
		{
			get
			{
				return this.connType;
			}
			set
			{
				this.connType = value;
			}
		}

		public PipeSet SupplyPipes
		{
			get
			{
				return this.supplyPipes;
			}
		}

		public PipeSet BackPipes
		{
			get
			{
				return this.backPipes;
			}
		}

		public IList<RisePipeSystem> RisePipes
		{
			get
			{
				return this.risePipes;
			}
		}

		public double Resistance
		{
			get
			{
				return this.resistance;
			}
		}

		public string WorstCircleInfo { get; set; }

		public void Add(RisePipeSystem rise)
		{
			this.risePipes.Add(rise);
		}

		public Dictionary<string, double> CalcUnbalance()
		{
			Dictionary<string, double> dictionary = new Dictionary<string, double>();
			foreach (RisePipeSystem risePipeSystem in this.risePipes)
			{
				risePipeSystem.CalcUnbalance();
				double num = Math.Abs((risePipeSystem.PressAcutal - this.resistance) / this.resistance);
				risePipeSystem.RateUnbalance = num;
				dictionary.Add(risePipeSystem.Name, num);
				Log.Info(new string[]
				{
					string.Format("rise {0} Resistance = {1} Unbalance={2}", risePipeSystem.Name, risePipeSystem.PressAcutal, num)
				});
			}
			return dictionary;
		}

		public int GetWorstRisesystemIndex()
		{
			this.worstIndex = (from rise in this.risePipes
			where rise.PressAcutal == (from r in this.risePipes
			select r.PressAcutal).Max()
			select rise.Index).First<int>();
			return this.worstIndex;
		}

		public RisePipeSystem GetAt(int risrIndex)
		{
			if (risrIndex > this.risePipes.Count)
			{
				return null;
			}
			return (from rise in this.risePipes
			where rise.Index == risrIndex
			select rise).First<RisePipeSystem>();
		}

		public void CalcRiserPressAcutal()
		{
			this.Update();
			for (int i = 0; i < this.risePipes.Count; i++)
			{
				RisePipeSystem at = this.GetAt(i + 1);
				if (at != null)
				{
					at.PressAcutal = at.CalcPressCutal();
					at.PressAcutal += this.supplyPipes.SumResistance(i + 1);
					if (this.connType == 0)
					{
						at.PressAcutal += this.backPipes.SumResistance(this.risePipes.Count - i);
					}
					else
					{
						at.PressAcutal += this.backPipes.SumResistance(i + 1);
					}
				}
			}
			this.resistance = (from rise in this.risePipes
			select rise.PressAcutal).Max();
			Log.Info(new string[]
			{
				this.Name + ": resistance = " + this.resistance
			});
		}

		public TreeNode ToTreeNode()
		{
			TreeNode treeNode = new TreeNode();
			treeNode.Tag = this;
			treeNode.Text = "室内系统";
			TreeNode treeNode2 = new TreeNode("总供水干管");
			treeNode2.Tag = this.SupplyPipes;
			TreeNode treeNode3 = new TreeNode("总回水干管");
			treeNode3.Tag = this.BackPipes;
			treeNode.Nodes.AddRange(new TreeNode[]
			{
				treeNode2,
				treeNode3
			});
			treeNode.Nodes.AddRange((from rise in this.risePipes
			select rise.ToTreeNode()).ToArray<TreeNode>());
			return treeNode;
		}

		public void Update()
		{
			this.supplyPipes.UpDate();
			this.backPipes.UpDate();
			this.risePipes.ForEach(delegate(RisePipeSystem rise)
			{
				rise.UpDate();
			});
		}

		public void BackRevit()
		{
			this.supplyPipes.BackRevit();
			this.backPipes.BackRevit();
			this.risePipes.ForEach(delegate(RisePipeSystem rise)
			{
				rise.BackRevit();
			});
		}

		private double resistance;

		private IList<RisePipeSystem> risePipes;

		private PipeSet supplyPipes;

		private PipeSet backPipes;

		private int connType = 1;

		private int worstIndex = 1;
	}
}
